package com.example.shopping_web_back.controller;

import com.example.shopping_web_back.pojo.Result;
import com.example.shopping_web_back.service.EmailService;
import jakarta.mail.MessagingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/email")
public class EmailController {

    @Autowired
    private EmailService emailService;

    @PostMapping("/sendVerificationEmail")
    public Result<String> sendEmail(@RequestParam String to) throws MessagingException {
        //生成四位数验证码
        String verificationCode = String.valueOf((int)(Math.random() * 9000) + 1000);
        String subject = "您的验证码";
        emailService.sendEmailWithVerificationCode(to, subject, verificationCode);
        return Result.success(verificationCode);
    }
}
